package com.example.testapplication;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.testapplication.greendao.DaoMaster;
import com.example.testapplication.greendao.DaoSession;
import com.example.testapplication.greendao.SearchHistoryDao;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SearchHistoryActivity extends AppCompatActivity {

    ImageView ivBack;
    EditTextWithDel etSearch;
    TextView tvSearch;
    TextView tvClear;
    List<SearchHistory> searchContentList;
    SearchHistoryDao searchHistoryDao;
    SearchHistoryAdapter searchHistoryAdapter;
    TagFlowLayout tagFlowLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_history);

        ivBack = findViewById(R.id.ivBack);
        etSearch = findViewById(R.id.etSearch);
        tvSearch = findViewById(R.id.tvSearch);
        tvClear = findViewById(R.id.tvClear);
        tagFlowLayout = findViewById(R.id.rcHistory);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initDao();
        initData();



        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etSearch.getText().length() > 0) {
                    SearchHistory name = new SearchHistory(null, etSearch.getText().toString());
                    searchHistoryDao.insertOrReplace(name);
                    initData();
                }
            }
        });

        tvClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchHistoryDao.deleteAll();
                searchContentList.clear();
                initData();
            }
        });


    }


    private void initDao() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "recluse-db");
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        searchHistoryDao = daoSession.getSearchHistoryDao();
        searchContentList = new ArrayList<>();

    }

    private void initData() {
        searchContentList = searchHistoryDao.queryBuilder().list();
        Collections.reverse(searchContentList);
        tagFlowLayout.setAdapter(new TagAdapter<SearchHistory>(searchContentList) {
            @Override
            public View getView(FlowLayout parent, int position, SearchHistory sh) {
                final TextView tv = (TextView) LayoutInflater.from(SearchHistoryActivity.this).inflate(R.layout.item_search_history,tagFlowLayout,false);
                tv.setText(sh.getHistory());
                return tv;
            }
        });
    }

}
